package com.itheima.advantagesup.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.advantagesup.dto.SysUserDto;
import com.itheima.advantagesup.entity.PageBean;
import com.itheima.advantagesup.entity.SysUser;
import com.itheima.advantagesup.mapper.SysUserMapper;
import com.itheima.advantagesup.service.SysUserService;
import com.itheima.advantagesup.vo.UserInfoVO;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * SysUserServiceImpl
 *
 * @author liliudong
 * @version 1.0
 * @description
 * @date 2023/7/26 14:58
 */
@Service
@RequiredArgsConstructor
public class SysUserServiceImpl implements SysUserService {

@Autowired
private SysUserMapper sysUserMapper;
    @Override
    public PageBean findAllUser(Integer page, Integer limit, String loginName, Integer status, String phone) {
        PageHelper.startPage(page,limit);
        List<SysUser> list=sysUserMapper.findAllUser(loginName,status,phone);
        Page<SysUser> p=(Page<SysUser>)list;
        ArrayList<UserInfoVO> arrayList = new ArrayList<>();

        //SysUser sysUser=new SysUser();
        for (SysUser sysUser : list) {
            String roleIdJsonArr = sysUser.getRoleId();
            UserInfoVO userInfoVO=new UserInfoVO();
            if (StringUtils.isNotBlank(roleIdJsonArr)) {
                JSONArray roleIds = JSON.parseArray(roleIdJsonArr);
                List<String> roleIdsJavaList = roleIds.toJavaList(String.class);
                userInfoVO.setRole_id(roleIdsJavaList);
                List<String> roleNameList = sysUserMapper.selectNameListByIds(roleIdsJavaList);
                userInfoVO.setRolename(roleNameList);
            }
            userInfoVO.setId(sysUser.getId());
            userInfoVO.setPassword(sysUser.getPassword());
            userInfoVO.setName(sysUser.getName());
            userInfoVO.setLogin_name(sysUser.getLogin_name());
            userInfoVO.setPhone(sysUser.getPhone());
            userInfoVO.setEmail(sysUser.getEmail());
            userInfoVO.setLogin_ip(sysUser.getLoginIp());
            userInfoVO.setLogin_date(sysUser.getLoginDate());
            userInfoVO.setStatus(sysUser.getStatus());
            userInfoVO.setDel_flag(sysUser.getDelFlag());
            userInfoVO.setCreate_date(sysUser.getCreate_date());
            userInfoVO.setUpdate_date(sysUser.getUpdateDate());

            arrayList.add(userInfoVO);
        }
        PageBean pageBean=new PageBean(p.getTotal(),p.getPageSize(),p.getPageNum(),p.getPages(),arrayList);
        return pageBean;
    }


    @Override
    public void delete(String id) {
        sysUserMapper.delete(id);
    }

    /**
     * 新增用户
     * @param sysUserDto
     */
    // todo 添加时候会报异常能正常添加，大概是因为权限没有添加
    @Override
    public void add(SysUserDto sysUserDto) {
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(sysUserDto,sysUser);
        sysUser.setId(UUID.randomUUID().toString());
        sysUser.setCreate_date(LocalDateTime.now());
        sysUser.setUpdateDate(LocalDateTime.now());
        String password = DigestUtils.md5DigestAsHex(sysUser.getPassword().getBytes());
        sysUser.setPassword(password);
        sysUser.setDelFlag("1");
        List<String> role_id = sysUserDto.getRole_id();
        JSONArray array = new JSONArray();
        for (String s : role_id) {
           array.add(s);

        }

        sysUser.setRoleId(array.toString());
        sysUserMapper.add(sysUser);
    }

    /**
     * 根据id回显
     * @param id
     * @return
     */
    @Override
    public UserInfoVO rowInfo(String id) {
        SysUser sysUser = sysUserMapper.selectById(id);
        UserInfoVO userInfoVO=new UserInfoVO();
        BeanUtils.copyProperties(sysUser,userInfoVO);
        userInfoVO.setPassword(null);
        userInfoVO.setLogin_name(sysUser.getLogin_name());
        if (StringUtils.isNotBlank(sysUser.getRoleId())) {
            JSONArray roleIds = JSON.parseArray(sysUser.getRoleId());
            List<String> roleIdsJavaList = roleIds.toJavaList(String.class);
            userInfoVO.setRole_id(roleIdsJavaList);
            List<String> roleNameList = sysUserMapper.selectNameListByIds(roleIdsJavaList);
            userInfoVO.setRolename(roleNameList);
        }
        return userInfoVO;
    }

    @Override
    public void update(SysUserDto sysUserDto) {
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(sysUserDto,sysUser);
       sysUser.setUpdateDate(LocalDateTime.now());
       if(sysUser.getPassword()!=null){
           String password = DigestUtils.md5DigestAsHex(sysUser .getPassword().getBytes());
           sysUser.setPassword(password);
       }
        sysUser.setDelFlag("1");
        List<String> role_id = sysUserDto.getRole_id();
        JSONArray array = new JSONArray();
        for (String s : role_id) {
            array.add(s);

        }
        sysUser.setRoleId(array.toString());
        sysUserMapper.update(sysUser);
    }

}
